import json
import re

def lua_to_json(lua_content):
    # Extracting the List content from the Lua file using regex

    print("Lua content:", lua_content)

    match = re.findall(r"LazyScript\[\"WARLOCK\"\] = {\n(.+?)}\nend", "".join(lua_content), re.DOTALL)
    if not match:
        raise ValueError("The provided Lua content does not match the expected format.")
    
    list_content = match[0]
    
    # Replace Lua table syntax with JSON syntax
    json_content = list_content.replace("[\"","\"").replace("\"]","\"").replace("]","").replace("[","")

    # Convert to JSON format
    json_content = json_content.replace("=", ":").replace("--", "//")

    print(json_content)

    return json_content

# Read the Lua file
with open('./test_lua/warlock.lua', 'r', encoding='utf-8') as lua_file:
    lua_content = lua_file.read()

# print(lua_content)

# Convert to JSON
json_data = lua_to_json(lua_content)

# Output the JSON data to a file
with open('./test_json/warlock.json', 'w', encoding='utf-8') as json_file:
    json.dump(json_data, json_file, ensure_ascii=False, indent=4)

print("Conversion complete. JSON data saved to warlock.json.")